home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / CPlusIncludes / PascalString.h < prev    next >
Text File  |  1991-05-01  |  14KB  |  666 lines

  1. #ifndef __PASCALSTRING__
  2. #define __PASCALSTRING__
  3.  
  4. #ifndef __STRING__
  5. #include "String.h"
  6. #endif
  7.  
  8. #ifndef __TYPES__
  9. #include "Types.h"
  10. #endif
  11.  
  12. // Forward declaration for all the string classes.
  13. struct String;
  14. struct Str255;
  15. struct Str63;
  16. struct Str32;
  17. struct Str31;
  18.  
  19. typedef const Str255& ConstStr255Param;
  20. typedef const Str63& ConstStr63Param;
  21. typedef const Str32& ConstStr32Param;
  22. typedef const Str31& ConstStr31Param;
  23.  
  24. #ifndef __OSUTILS__
  25. #include "OSUtils.h"
  26. #endif
  27.  
  28. typedef unsigned char SimpleStr255[256], SimpleStr63[64], SimpleStr32[33],
  29.                       SimpleStr31[32], SimpleStr27[28], SimpleStr15[16], * SimpleStringPtr, ** SimpleStringHandle;
  30.  
  31. // Some constants defining the length of each of the string types.
  32.  
  33. const short kLengthByte = 1;
  34. const short kBaseLen = 2;
  35. const short kStr255Len = 255;
  36. const short kStr63Len = 63;
  37. const short kStr32Len = 32;
  38. const short kStr31Len = 31;
  39.  
  40. // Some external function declarations so that we don't have to include more than
  41. // the minimal set of header files.
  42.  
  43. pascal char* PLSTRSTR(const String& str1,
  44.                       const String& str2);        // From PaslibIntf.p
  45.  
  46. typedef struct String *StringPtr, ** StringHandle;
  47. struct String
  48. {
  49. public:
  50.     unsigned char fStr[kBaseLen];
  51.  
  52. protected:
  53.     void InsertHelper(const String& insStr,
  54.                       short pos,
  55.                       short maxLength);
  56.     void InsertHelper(const char* insStr,
  57.                       short pos,
  58.                       short maxLength);
  59.  
  60. public:
  61.  
  62.     // Basic length method, inherited by all derived classes. Define one that returns a
  63.     // reference. Can be used as an lvalue and only can be applied to non-const Strings.
  64.  
  65.     inline unsigned char& Length()
  66.     {
  67.         return fStr[0];
  68.     }                                            // for non-const String
  69.  
  70.  
  71.     inline unsigned char Length() const
  72.     {
  73.         return fStr[0];
  74.     }                                            // for const String
  75.  
  76.  
  77.     inline Boolean IsEmpty()
  78.     {
  79.         return fStr[0] <= 0;
  80.     }
  81.  
  82.     inline Boolean IsEmpty() const
  83.     {
  84.         return fStr[0] <= 0;
  85.     }
  86.  
  87.     // Character selector operator.
  88.  
  89.     unsigned char& operator[](short pos);            // for non-const String
  90.                                                     // !!! try to inline later
  91.  
  92.     inline unsigned char operator[](short pos) const
  93.     {
  94.         return fStr[pos];
  95.     }                                            // for const String
  96.     
  97.     // Return the String as a CString. Used in debugging to fprintf a String.
  98.     
  99.     operator char*() const;
  100.  
  101.     // Relational operators that are inherited by all the derived string types. Three of
  102.     // each so that literal C Strings can be conveniently used for one of the operators as
  103.     // well as two of the derive classes as operators. These are declared here but defined
  104.     // below all the string classes because they use constructors for Str255 and its class
  105.     // definition has not been encountered yet.
  106.  
  107.     inline friend Boolean operator==(const String& s1,
  108.                                      const char* s2);
  109.     inline friend Boolean operator==(const char* s1,
  110.                                      const String& s2);
  111.     inline friend Boolean operator==(const String& s1,
  112.                                      const String& s2);
  113.  
  114.     inline friend Boolean operator!=(const String& s1,
  115.                                      const char* s2);
  116.     inline friend Boolean operator!=(const char* s1,
  117.                                      const String& s2);
  118.     inline friend Boolean operator!=(const String& s1,
  119.                                      const String& s2);
  120.  
  121.     inline friend Boolean operator>(const String& s1,
  122.                                     const char* s2);
  123.     inline friend Boolean operator>(const char* s1,
  124.                                     const String& s2);
  125.     inline friend Boolean operator>(const String& s1,
  126.                                     const String& s2);
  127.  
  128.     inline friend Boolean operator<(const String& s1,
  129.                                     const char* s2);
  130.     inline friend Boolean operator<(const char* s1,
  131.                                     const String& s2);
  132.     inline friend Boolean operator<(const String& s1,
  133.                                     const String& s2);
  134.  
  135.     inline friend Boolean operator>=(const String& s1,
  136.                                      const char* s2);
  137.     inline friend Boolean operator>=(const char* s1,
  138.                                      const String& s2);
  139.     inline friend Boolean operator>=(const String& s1,
  140.                                      const String& s2);
  141.  
  142.     inline friend Boolean operator<=(const String& s1,
  143.                                      const char* s2);
  144.     inline friend Boolean operator<=(const char* s1,
  145.                                      const String& s2);
  146.     inline friend Boolean operator<=(const String& s1,
  147.                                      const String& s2);
  148.  
  149.     // Concatenation operator that are inherited by all the derived string types. Three
  150.     // of each so that literal C Strings can be conveniently used for one of the operators
  151.     // as well as using any two classes derived from String.
  152.  
  153.     friend Str255 operator+(const String& s1,
  154.                             const char* s2);
  155.     friend Str255 operator+(const char* s1,
  156.                             const String& s2);
  157.     friend Str255 operator+(const String& s1,
  158.                             const String& s2);
  159.  
  160.     // Methods that mimic the Pascal builtin string functions for Pos, Insert and Delete.
  161.     // Note that insert and copy is implemented in the derived classes.
  162.  
  163.     inline unsigned char Pos(const char* subStr);
  164.     inline unsigned char Pos(const String& subStr);
  165.     inline void Delete(short pos,
  166.                        short length);
  167. };
  168.  
  169.  
  170. struct Str255 : String
  171. {
  172.  
  173.     friend struct Str63;
  174.     friend struct Str31;
  175.  
  176. private:
  177.     unsigned char fData[kStr255Len - 1];
  178.  
  179. public:
  180.     inline Str255();
  181.     inline Str255(const Str255& str);
  182.     inline Str255(const Str63& str);
  183.     inline Str255(const Str31& str);
  184.     Str255(const char* str);
  185.     inline Str255(const SimpleStr255& str);
  186.  
  187.     // Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
  188.     
  189.     inline void Insert(const String& str,
  190.                        short pos);
  191.     inline void Insert(const char* str,
  192.                        short pos);
  193.     Str255 Copy(short pos, short length);
  194.                        
  195.     // Concatenation operator
  196.     
  197.     Str255& operator +=(const String& str);
  198.     Str255& operator +=(const char* str);
  199.     Str255& operator +=(const char ch);
  200. };
  201.  
  202.  
  203. struct Str63 : String
  204. {
  205.  
  206.     friend struct Str255;
  207.     friend struct Str31;
  208.  
  209. private:
  210.     unsigned char fData[kStr63Len - 1];
  211.  
  212. public:
  213.     inline Str63();
  214.     inline Str63(const Str255& str);
  215.     inline Str63(const Str63& str);
  216.     inline Str63(const Str31& str);
  217.     inline Str63(const SimpleStr63& str);
  218.     Str63(const char* str);
  219.  
  220.     // Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
  221.     
  222.     inline void Insert(const String& str,
  223.                        short pos);
  224.     inline void Insert(const char* str,
  225.                        short pos);
  226.     Str63 Copy(short pos, short length);
  227.  
  228.     // Concatenation operator
  229.     
  230.     Str63& operator +=(const String& str);
  231.     Str63& operator +=(const char* str);
  232.     Str63& operator +=(const char ch);
  233. };
  234.  
  235.  
  236. struct Str32 : String
  237. {
  238.  
  239.     friend struct Str255;
  240.     friend struct Str63;
  241.  
  242. private:
  243.     unsigned char fData[kStr32Len - 1];
  244.  
  245. public:
  246.     inline Str32();
  247.     inline Str32(unsigned char length)
  248.     {
  249.         fStr[0] = length;
  250.     }
  251.  
  252.  
  253.     inline Str32(const Str255& str);
  254.     inline Str32(const Str63& str);
  255.     inline Str32(const Str32& str);
  256.     inline Str32(const SimpleStr32& str);
  257.     Str32(const char* str);
  258.  
  259.  
  260.     // Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
  261.     
  262.     inline void Insert(const String& str,
  263.                        short pos);
  264.     inline void Insert(const char* str,
  265.                        short pos);
  266.     Str32 Copy(short pos, short length);
  267.  
  268.     // Concatenation operator
  269.     
  270.     Str32& operator +=(const String& str);
  271.     Str32& operator +=(const char* str);
  272.     Str32& operator +=(const char ch);
  273. };
  274.  
  275.  
  276. struct Str31 : String
  277. {
  278.  
  279.     friend struct Str255;
  280.     friend struct Str63;
  281.     friend struct Str32;
  282.  
  283. private:
  284.     unsigned char fData[kStr31Len - 1];
  285.  
  286. public:
  287.     inline Str31();
  288.     inline Str31(unsigned char length)
  289.     {
  290.         fStr[0] = length;
  291.     }
  292.  
  293.  
  294.     inline Str31(const Str255& str);
  295.     inline Str31(const Str63& str);
  296.     inline Str31(const Str32& str);
  297.     inline Str31(const Str31& str);
  298.     inline Str31(const SimpleStr31& str);
  299.     Str31(const char* str);
  300.  
  301.     // Insert and Copy roughly equivalent to the Pascal Insert and Copy functions.
  302.     
  303.     inline void Insert(const String& str,
  304.                        short pos);
  305.     inline void Insert(const char* str,
  306.                        short pos);
  307.     Str31 Copy(short pos, short length);
  308.  
  309.     // Concatenation operator
  310.     
  311.     Str31& operator +=(const String& str);
  312.     Str31& operator +=(const char* str);
  313.     Str31& operator +=(const char ch);
  314. };
  315.  
  316.  
  317. // Function definitions for relational operators.
  318.  
  319. inline Boolean operator==(const String& s1,
  320.                           const char* s2)
  321. {
  322.     return RelString(s1, Str255(s2), false, true) == 0;
  323. }
  324.  
  325.  
  326. inline Boolean operator==(const char* s1,
  327.                           const String& s2)
  328. {
  329.     return RelString(Str255(s1), s2, false, true) == 0;
  330. }
  331.  
  332.  
  333. inline Boolean operator==(const String& s1,
  334.                           const String& s2)
  335. {
  336.     return RelString(s1, s2, false, true) == 0;
  337. }
  338.  
  339.  
  340. inline Boolean operator!=(const String& s1,
  341.                           const char* s2)
  342. {
  343.     return RelString(s1, Str255(s2), false, true) != 0;
  344. }
  345.  
  346.  
  347. inline Boolean operator!=(const char* s1,
  348.                           const String& s2)
  349. {
  350.     return RelString(Str255(s1), s2, false, true) != 0;
  351. }
  352.  
  353.  
  354. inline Boolean operator!=(const String& s1,
  355.                           const String& s2)
  356. {
  357.     return RelString(s1, s2, false, true) != 0;
  358. }
  359.  
  360.  
  361. inline Boolean operator>(const String& s1,
  362.                          const char* s2)
  363. {
  364.     return RelString(s1, Str255(s2), false, true) > 0;
  365. }
  366.  
  367.  
  368. inline Boolean operator>(const char* s1,
  369.                          const String& s2)
  370. {
  371.     return RelString(Str255(s1), s2, false, true) > 0;
  372. }
  373.  
  374.  
  375. inline Boolean operator>(const String& s1,
  376.                          const String& s2)
  377. {
  378.     return RelString(s1, s2, false, true) > 0;
  379. }
  380.  
  381.  
  382. inline Boolean operator<(const String& s1,
  383.                          const char* s2)
  384. {
  385.     return RelString(s1, Str255(s2), false, true) < 0;
  386. }
  387.  
  388.  
  389. inline Boolean operator<(const char* s1,
  390.                          const String& s2)
  391. {
  392.     return RelString(Str255(s1), s2, false, true) < 0;
  393. }
  394.  
  395.  
  396. inline Boolean operator<(const String& s1,
  397.                          const String& s2)
  398. {
  399.     return RelString(s1, s2, false, true) < 0;
  400. }
  401.  
  402.  
  403. inline Boolean operator>=(const String& s1,
  404.                           const char* s2)
  405. {
  406.     return RelString(s1, Str255(s2), false, true) >= 0;
  407. }
  408.  
  409.  
  410. inline Boolean operator>=(const char* s1,
  411.                           const String& s2)
  412. {
  413.     return RelString(Str255(s1), s2, false, true) >= 0;
  414. }
  415.  
  416.  
  417. inline Boolean operator>=(const String& s1,
  418.                           const String& s2)
  419. {
  420.     return RelString(s1, s2, false, true) >= 0;
  421. }
  422.  
  423.  
  424. inline Boolean operator<=(const String& s1,
  425.                           const char* s2)
  426. {
  427.     return RelString(s1, Str255(s2), false, true) <= 0;
  428. }
  429.  
  430.  
  431. inline Boolean operator<=(const char* s1,
  432.                           const String& s2)
  433. {
  434.     return RelString(Str255(s1), s2, false, true) <= 0;
  435. }
  436.  
  437.  
  438. inline Boolean operator<=(const String& s1,
  439.                           const String& s2)
  440. {
  441.     return RelString(s1, s2, false, true) <= 0;
  442. }
  443.  
  444.  
  445. // Function definitions for Str255 constructors.
  446.  
  447. inline Str255::Str255()
  448. {
  449. }
  450.  
  451.  
  452. inline Str255::Str255(const Str255& str)
  453. {
  454.     memcpy(fStr, str.fStr, str.Length() + kLengthByte);
  455. }
  456.  
  457.  
  458. inline Str255::Str255(const Str63& str)
  459. {
  460.     memcpy(fStr, str.fStr, str.Length() + kLengthByte);
  461. }
  462.  
  463.  
  464. inline Str255::Str255(const Str31& str)
  465. {
  466.     memcpy(fStr, str.fStr, str.Length() + kLengthByte);
  467. }
  468.  
  469.  
  470. inline Str255::Str255(const SimpleStr255& str)
  471. {
  472.     memcpy(fStr, str, kStr255Len + kLengthByte);
  473. }
  474.  
  475. // Fuction definitions for Str63 constructors.
  476.  
  477. inline Str63::Str63()
  478. {
  479. }
  480.  
  481.  
  482. inline Str63::Str63(const Str255& str)
  483. {
  484.     // Truncate the Str255 to 63 bytes if necessary.
  485.  
  486.     Length() = str.Length() > kStr63Len ? kStr63Len : str.Length();
  487.     memcpy(fStr, str.fStr, Length() + kLengthByte);
  488. }
  489.  
  490.  
  491. inline Str63::Str63(const Str63& str)
  492. {
  493.     memcpy(fStr, str.fStr, str.Length() + kLengthByte);
  494. }
  495.  
  496.  
  497. inline Str63::Str63(const Str31& str)
  498. {
  499.     memcpy(fStr, str.fStr, str.Length() + kLengthByte);
  500. }
  501.  
  502.  
  503. inline Str63::Str63(const SimpleStr63& str)
  504. {
  505.     memcpy(fStr, str, kStr63Len + kLengthByte);
  506. }
  507.  
  508. // Function definitions for Str32 constructors.
  509.  
  510. inline Str32::Str32()
  511. {
  512. }
  513.  
  514.  
  515. inline Str32::Str32(const Str255& str)
  516. {
  517.     // Truncate the Str255 to 32 bytes if necessary.
  518.  
  519.     Length() = str.Length() > kStr32Len ? kStr32Len : str.Length();
  520.     memcpy(fStr, str.fStr, Length() + kLengthByte);
  521. }
  522.  
  523.  
  524. inline Str32::Str32(const Str63& str)
  525. {
  526.     // Truncate the Str63 to 32 bytes if necessary.
  527.  
  528.     Length() = str.Length() > kStr32Len ? kStr32Len : str.Length();
  529.     memcpy(fStr, str.fStr, Length() + kLengthByte);
  530. }
  531.  
  532.  
  533. inline Str32::Str32(const Str32& str)
  534. {
  535.     memcpy(fStr, str.fStr, str.Length() + kLengthByte);
  536. }
  537.  
  538.  
  539. inline Str32::Str32(const SimpleStr32& str)
  540. {
  541.     memcpy(fStr, str, kStr31Len + kLengthByte);
  542. }
  543.  
  544.  
  545. // Function definitions for Str31 constructors.
  546.  
  547. inline Str31::Str31()
  548. {
  549. }
  550.  
  551.  
  552. inline Str31::Str31(const Str255& str)
  553. {
  554.     // Truncate the Str255 to 31 bytes if necessary.
  555.  
  556.     Length() = str.Length() > kStr31Len ? kStr31Len : str.Length();
  557.     memcpy(fStr, str.fStr, Length() + kLengthByte);
  558. }
  559.  
  560.  
  561. inline Str31::Str31(const Str63& str)
  562. {
  563.     // Truncate the Str63 to 31 bytes if necessary.
  564.  
  565.     Length() = str.Length() > kStr31Len ? kStr31Len : str.Length();
  566.     memcpy(fStr, str.fStr, Length() + kLengthByte);
  567. }
  568.  
  569.  
  570. inline Str31::Str31(const Str31& str)
  571. {
  572.     memcpy(fStr, str.fStr, str.Length() + kLengthByte);
  573. }
  574.  
  575.  
  576. inline Str31::Str31(const SimpleStr31& str)
  577. {
  578.     memcpy(fStr, str, kStr31Len + kLengthByte);
  579. }
  580.  
  581. // Function definitions for methods to mimic the Pascal builtin string operators.
  582.  
  583. inline unsigned char String::Pos(const char* subStr)
  584. {
  585.     char* ptr;
  586.  
  587.     ptr = strstr((const char*) & fStr[1], subStr);
  588.     return ptr != NULL ? ptr - (char*)fStr : 0;
  589. }
  590.  
  591.  
  592. inline unsigned char String::Pos(const String& subStr)
  593. {
  594.     char* ptr;
  595.  
  596.     ptr = PLSTRSTR(*this, subStr);
  597.     return ptr != NULL ? ptr - (char*)fStr : 0;
  598. }
  599.  
  600.  
  601. inline void String::Delete(short pos,
  602.                            short length)
  603. {
  604.     memcpy(&fStr[pos], &fStr[pos + length], Length() - (pos + length) + kLengthByte);
  605.     fStr[0] -= length;
  606. }
  607.  
  608.  
  609. inline void Str255::Insert(const String& str,
  610.                            short pos)
  611. {
  612.     InsertHelper(str, pos, kStr255Len);
  613. }
  614.  
  615.  
  616. inline void Str255::Insert(const char* str,
  617.                            short pos)
  618. {
  619.     InsertHelper(str, pos, kStr255Len);
  620. }
  621.  
  622.  
  623. inline void Str63::Insert(const String& str,
  624.                           short pos)
  625. {
  626.     InsertHelper(str, pos, kStr63Len);
  627. }
  628.  
  629.  
  630. inline void Str63::Insert(const char* str,
  631.                           short pos)
  632. {
  633.     InsertHelper(str, pos, kStr63Len);
  634. }
  635.  
  636.  
  637. inline void Str32::Insert(const String& str,
  638.                           short pos)
  639. {
  640.     InsertHelper(str, pos, kStr32Len);
  641. }
  642.  
  643.  
  644. inline void Str32::Insert(const char* str,
  645.                           short pos)
  646. {
  647.     InsertHelper(str, pos, kStr32Len);
  648. }
  649.  
  650.  
  651. inline void Str31::Insert(const String& str,
  652.                           short pos)
  653. {
  654.     InsertHelper(str, pos, kStr31Len);
  655. }
  656.  
  657.  
  658. inline void Str31::Insert(const char* str,
  659.                           short pos)
  660. {
  661.     InsertHelper(str, pos, kStr31Len);
  662. }
  663.  
  664. #endif
  665.  
  666.